package cn.dapeng.kafka.config;

import cn.dapeng.kafka.message.KafkaMessageOperate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.util.Assert;

/**
 * @BelongProject message-queue
 * @BelongPackage cn.dapeng.rocket.config
 * @Description TODO
 * @ClassName KafkaAutoConfiguration
 * @Author xupenghong
 * @Date 2024/6/28 10:04
 * @Version 1.0.0
 */
@Configuration
@ConditionalOnProperty(prefix = "kafka-mq", value = "name-server", matchIfMissing = true)
@EnableConfigurationProperties({KafkaProperties.class, IfUseKafkaProperties.class})
public class KafkaAutoConfiguration {

    @Bean
    public KafkaTemplate kafkaTemplate(KafkaProperties kafkaProperties, IfUseKafkaProperties ifUseKafkaProperties) {
        if (ifUseKafkaProperties.isUse()) {
            Assert.hasText(kafkaProperties.getTopic(), "topic must not be null");
            Assert.hasText(kafkaProperties.getNameServer(), "nameServer must not be null");

            KafkaTemplate kafkaTemplate = new KafkaTemplate();
            kafkaTemplate.setNameServer(kafkaProperties.getNameServer());
            return kafkaTemplate;
        }

        return null;
    }

    @Bean
    @DependsOn({"kafkaTemplate"})
    public KafkaMessageOperate kafkaMessageOperate(KafkaTemplate kafkaTemplate) {
        return new KafkaMessageOperate(kafkaTemplate);
    }

}
